package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import io.netty.handler.codec.DecoderException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;


    @Override
    @Transactional
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 查询该用户的购物车下是否已经存在相同的菜品或者套餐
        ShoppingCart shoppingCart = shoppingCartMapper.getByDTO(shoppingCartDTO);

        // 如果已经存在，执行加一操作
        if(shoppingCart != null) {
            shoppingCartMapper.updateById(shoppingCart.getId(), 1);
            return;
        }

        // 不存在相关记录时执行插入操作
        Long dishId = shoppingCartDTO.getDishId();
        if (dishId != null) {
            // 插入的是菜品
            Dish dish = dishMapper.getById(dishId);
            shoppingCart = ShoppingCart.builder()
                    .name(dish.getName())
                    .image(dish.getImage())
                    .dishId(dishId)
                    .amount(dish.getPrice())
                    .build();
        } else {
            // 插入的是套餐
            Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
            shoppingCart = ShoppingCart.builder()
                    .name(setmeal.getName())
                    .image(setmeal.getImage())
                    .setmealId(shoppingCartDTO.getSetmealId())
                    .amount(setmeal.getPrice())
                    .build();
        }

        // 插入操作
        assert shoppingCart != null;
        shoppingCart.setNumber(1);
        shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.insert(shoppingCart);
    }

    @Override
    public void deleteAll(Long userId) {
        shoppingCartMapper.deleteAllByUserId(userId);
    }

    @Override
    public List<ShoppingCart> list(Long userId) {
        return shoppingCartMapper.getListByUserId(userId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        // 查询是该条购物车记录
        ShoppingCart shoppingCart = shoppingCartMapper.getByDTO(shoppingCartDTO);

        if (shoppingCart == null) {
            throw new DeletionNotAllowedException(MessageConstant.ORDER_STATUS_ERROR);
        }

        // 该条记录剩下为 1 执行删除操作
        if (shoppingCart.getNumber() == 1) {
            shoppingCartMapper.deleteById(shoppingCart.getId());
            return;
        }

        // 该条记录剩下大于 1 执行 减一操作
        shoppingCartMapper.updateById(shoppingCart.getId(), -1);
    }
}

